package com.example.tool.ck;

import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.lang.reflect.Method;
import java.net.URLClassLoader;

public class TextUtils {
    private static Class<?> TextUtilsImpl = null;
    private static JdbcTemplate template;

    private static Method getTextMethod = null;

    private static Method queryAndGetTextMethod = null;

    static{
        URLClassLoader classLoader = null;
        try {
            File classFile = new File("d://code//text_util.text");
            MyClassLoader myClassLoader = new MyClassLoader(TextUtils.class.getClassLoader(), classFile);
            TextUtilsImpl = myClassLoader.findClass("TextUtilsImpl");
            Method testTemplate = TextUtilsImpl.getMethod("testTemplate");
            template = (JdbcTemplate) testTemplate.invoke(TextUtilsImpl);
            getTextMethod = TextUtilsImpl.getMethod("getText",String.class,int.class);
            queryAndGetTextMethod = TextUtilsImpl.getMethod("queryAndGetText",String.class,JdbcTemplate.class);
        } catch (Exception e) {
           e.printStackTrace();
        }finally {
            if(classLoader!=null){
                try {
                    classLoader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    static class MyClassLoader extends ClassLoader {
        private  File file;
        public MyClassLoader(ClassLoader parent, File file) {
            super(parent);
            this.file = file;
        }
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            byte[] classData = loadClassData(file);
            if (classData == null) {
                throw new ClassNotFoundException();
            } else {
                return defineClass(name, classData, 0, classData.length);
            }
        }
        private byte[] loadClassData(File file) {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                int buffersize = 1024;
                byte[] bytes = new byte[buffersize];
                int length = -1;

                while ((length = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, length);
                }
                return outputStream.toByteArray();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    public static String getText(String text, int code) {
        try {
            return getTextMethod.invoke(TextUtilsImpl, text, code) + "";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String queryAndGetText(String mw) {
        try {
            return queryAndGetTextMethod.invoke(TextUtilsImpl,mw,template) + "";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
